package es.efor.ics.gev.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 * The persistent class for the <b>HORARIOS</b> database table.
 * 
 * @author dlisbona
 *
 */
@Entity
@Table(name = "HORARIOS")
public class Horario extends BaseObject implements Serializable {

	// static data
	private static final long serialVersionUID = 3945761102748176186L;

	// private instance data
	private Long id;
	private String descripcion;
	private List<DiaLectivo> diasLectivos = new ArrayList<DiaLectivo>();
	private Integer version;
	
	// ********************** Accessor Methods ********************** //
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "HORARIO_ID")
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "DESCRIPCION", length = 255, nullable = false)
	public String getDescripcion() {
		return descripcion;
	}

	public void setDescripcion(String descripcion) {
		this.descripcion = descripcion;
	}

	@OneToMany(mappedBy = "horario", cascade = { CascadeType.PERSIST,
			CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@org.hibernate.annotations.Cascade(value = {
			org.hibernate.annotations.CascadeType.SAVE_UPDATE,
			org.hibernate.annotations.CascadeType.DELETE,
			org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	public List<DiaLectivo> getDiasLectivos() {
		return diasLectivos;
	}

	public void setDiasLectivos(List<DiaLectivo> diasLectivos) {
		this.diasLectivos = diasLectivos;
	}
	
	public boolean addDiaLectivo(DiaLectivo diaLectivo) {
		if (diaLectivo == null)
			throw new IllegalArgumentException("Null diaLectivo!");
		if (this.diasLectivos.add(diaLectivo)) {
			diaLectivo.setHorario(this);
			return true;
		}
		return false;
	}
	
	public boolean removeDiaLectivo(DiaLectivo diaLectivo) {
		if (diaLectivo == null)
			throw new IllegalArgumentException("Null diaLectivo!");
		if (this.diasLectivos.remove(diaLectivo)) {
			diaLectivo.setHorario(null);
			return true;
		}
		return false;
	}

	public void removeAllDiasLectivos(boolean update) {
		for (DiaLectivo diaLectivo : diasLectivos) {
			if (update) {
				diaLectivo.setHorario(null);
			}
		}
		diasLectivos.clear();
	}
	
	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
	// ********************** Common Methods ********************** //
	
	@Override
	public boolean equals(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

}
